package org.hackystat.devcathlon.engine.event.impl;

import static org.junit.Assert.assertEquals;
import org.hackystat.devcathlon.configuration.ConfigurationManager;
import org.hackystat.devcathlon.engine.event.Event;
import org.hackystat.devcathlon.engine.event.EventScore;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.sensorbase.resource.projects.jaxb.Project;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests the Keep The Repository Clean Daily event.
 * @author robin
 *
 */
public class TestKeepTheRepositoryCleanDaily extends DevcathlonTestHelper {

  private static int successWeight = 5;
  private static int churnThreshold = 50;
  private boolean success = true;
  
  private String teamName = "TestTeam";
  private String teamOwner = "TeamOwner";
  private String matchName = "TestMatch";
  private String member1 = "Member1";
  private String member2 = "Member2";
  private String member3 = "Member3";
 
  /**
   * Sets up the DevcathlonConfiguration for use in these test cases. 
   * @throws Exception If problems occur setting up the configuration. 
   */
  @BeforeClass
  public static void setupConfiguration() throws Exception {
    
    // Set up the testing DevcathlonConfiguration for the event EveryoneBuilds.
    String eventName = KeepTheRepositoryCleanDaily.eventName;
    ConfigurationManager configManager = ConfigurationManager.getInstance();
    configManager.setBaseScoreWeight(eventName, 5);
    configManager.getConfiguration(eventName);
    configManager.setWakeupInterval(eventName, 1440);
    configManager.addProperty(eventName, KeepTheRepositoryCleanDaily.successWeightKey, 
        String.valueOf(successWeight));
    configManager.addProperty(eventName, KeepTheRepositoryCleanDaily.churnThresholdKey, 
        String.valueOf(churnThreshold));
  }
  
  /**
   * Tests this event when there is no build for this team.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testNoBuildData() throws Exception {
    // Create the team.
    Team team = makeTeam(teamName, teamOwner, member1, member2, member3);
    
    // Set the event instance.
    Event event = new KeepTheRepositoryCleanDaily();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    EventScore score = event.computeScore(match, team, Tstamp.makeTimestamp());
    assertEquals("Check base 1", 0, score.getBaseScore());
    assertEquals("Check points 1", 0, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is a single failed build for this team.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testFailedBuildData() throws Exception {
    // Create the team.
    Team team = makeTeam(teamName, teamOwner, member1, member2, member3);
    
    // Set the event instance.
    Event event = new KeepTheRepositoryCleanDaily();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send a single build data. 
    makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), false);  
    
    EventScore score = event.computeScore(match, team, Tstamp.makeTimestamp());
    assertEquals("Check base 2", 0, score.getBaseScore());
    assertEquals("Check points 2", 0, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is a single successful build for this team.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testSuccessfulBuildData() throws Exception {
    // Create the team.
    Team team = makeTeam(teamName, teamOwner, member1, member2, member3);
    
    // Set the event instance.
    Event event = new KeepTheRepositoryCleanDaily();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send a single build data. 
    makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), success);    
    
    EventScore score = event.computeScore(match, team, Tstamp.makeTimestamp());
    assertEquals("Check base 3", 0, score.getBaseScore());
    assertEquals("Check points 3", 0, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is a successful build and no churn for this team.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testSuccessBuildCommitNoChurn() throws Exception {
 // Create the team.
    Team team = makeTeam(teamName, teamOwner, member1, member2, member3);
    
    // Set the event instance.
    Event event = new KeepTheRepositoryCleanDaily();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send a single build and commit data. 
    makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), success);
    makeCommitData(team.getOwner(), project, Tstamp.makeTimestamp(), "0", "0");
    
    
    EventScore score = event.computeScore(match, team, Tstamp.makeTimestamp());
    assertEquals("Check base 4", 0, score.getBaseScore());
    assertEquals("Check points 4", 0, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is a successful build and not enoguh churn for this team.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testSuccessBuildCommitLessChurn() throws Exception {
 // Create the team.
    Team team = makeTeam(teamName, teamOwner, member1, member2, member3);
    
    // Set the event instance.
    Event event = new KeepTheRepositoryCleanDaily();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send a single build data. 
    makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), success);
    makeCommitData(team.getOwner(), project, Tstamp.makeTimestamp(), "40", "9");
    
    
    EventScore score = event.computeScore(match, team, Tstamp.makeTimestamp());
    assertEquals("Check base 5", 0, score.getBaseScore());
    assertEquals("Check points 5", 0, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
  
  /**
   * Tests this event when there is a successful build and churn for this team.
   * @throws Exception If there is a bug.
   */
  @Test
  public void testSuccessBuildCommitChurn() throws Exception {
 // Create the team.
    Team team = makeTeam(teamName, teamOwner, member1, member2, member3);
    
    // Set the event instance.
    Event event = new KeepTheRepositoryCleanDaily();
    
    // Create a simple match containing a single team, a single project, and this event.
    Project project = getProject(teamOwner, teamName);
    Match match = addSimpleMatch(matchName, team, project, event);
    
    // Send a single build data. 
    makeBuildData(team.getOwner(), project, Tstamp.makeTimestamp(), success);
    makeCommitData(team.getOwner(), project, Tstamp.makeTimestamp(), "40", "40");
    
    
    EventScore score = event.computeScore(match, team, Tstamp.makeTimestamp());
    assertEquals("Check base 5", 5, score.getBaseScore());
    assertEquals("Check points 5", 5, event.computePoints(score));
    
    // Cleanup.
    deleteTeam(teamName);
    deleteMatch(matchName);
  }
}
